// 导入模块
// import Vue from 'vue';
import axios from 'axios';
import {SERVER_BASE_URL, SERVER_CPT_URL} from './config';
import router from '@/router';
import iView, {Notice} from 'iview';
import qs from 'qs';
import store from '@/store';
// import {getStore, setStore, Decrypt, Encrypt} from '@/common/utils';
import {getStore} from '@/common/utils';

// 设置用户信息action
/* const setUserInfo = function (user) {
 //store.dispatch(SET_USER_INFO, user)
 }; */


export function fetch(options) {
    return new Promise((resolve, reject) => {
        let instance = axios.create({
            baseURL: SERVER_BASE_URL,
            timeout: 28000,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        if (!getStore('token')) {
            router.replace({
                name: 'login'
            });
            return;
        }
        // 加密数据
        // let tsu = {
        //   tdata: Encrypt(options.param, getStore('token').substr(5, 16))
        // }
        let tsu = {
            tdata: JSON.stringify(options.params)
        };
        options.data = qs.stringify(tsu);
        // console.log("options.data:",options.data)
        // if(options.param){
        //   let ss=Decrypt(tsu.tdata,  getStore('token').substr(5, 16))
        //   console.log("ss:",ss)
        // }
        options.params = {};

        instance.interceptors.request.use((config => {
            iView.LoadingBar.start();
            // config.headers = {
            //   'Content-Type': 'application/x-www-form-urlencoded'
            // }
            // 添加token
            if (getStore('token')) {
                config.headers.Authorization = getStore('token');
            }
            return config;
        }))

        instance.interceptors.response.use(response => {
            iView.LoadingBar.finish();
            if (response.data.jdata) {
                // let aeskey = getStore('token').substr(5, 16)
                let ciphertext = response.data.jdata;
                // let tsu = Decrypt(ciphertext, aeskey)
                let tsu = JSON.parse(ciphertext);
                store.commit('SET_TOKEN', response.data.token);
                response.data.Data = tsu;
            }
            return response;

        }, err => {
            iView.LoadingBar.error();
            return err
        })
        instance(options).then(response => {
            if (response.data.code === 0 ) {
                resolve(response.data)
            } else {
                Notice.error({
                    title: '出错了！',
                    desc: '错误原因 ' + JSON.stringify(response.data.message),
                    duration: 5
                });
                if (response.data.status === 401) {
                    router.replace({
                        name: 'login'
                    });
                }
                reject("error response status code")
            }
        }, err => {
            Notice.error({
                title: '出错了！',
                desc: err,
                duration: 5
            });
            reject(err)
        })
    })
}



// 非加密后端数据请求
export function fetchCPT(options) {
    return new Promise((resolve, reject) => {
        let instance = axios.create({
            baseURL: SERVER_CPT_URL,
            timeout: 5000,
            headers: {}
        });
        // http request 拦截器
        instance.interceptors.request.use(config => {
            iView.LoadingBar.start();
            config.headers = {
                'Content-Type': 'application/x-www-form-urlencoded'
            };
            // config.headers.Authorization = 'token'return config;
            if (getStore('token')) {
                config.headers.Authorization = getStore('token');
            }
            return config
        });
        instance.interceptors.response.use(response => {
            // 请求成功时,根据业务判断状态
            if (response.data.code === 0) {
                iView.LoadingBar.finish();
            } else {
                iView.LoadingBar.error();
            }
            return response;
        }, err => {
            iView.LoadingBar.error();
            return err;
        })

        instance(options).then(res => {
            if (res.data.code ===0 ) {
                resolve(res.data);
            } else {
                if (res.data.status === 401) {
                    router.replace({
                        name: 'login'
                    });
                }
                reject("error response status code")
            }
        }, err => {
            reject(err)
        })

    })
}